package com.dpf.api.utills;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateHelper {

    //默认格式
    private static String datePattern = "yyyy-MM-dd";
    private static String datePattern2 = "yyyy-MM";
    private static String datePattern3 = "yyyy/MM/dd";
    private static String datePattern4 = "yy/MM/dd";
    private static String datePattern5 = "yyyy年MM月dd日";
    private static String timePattern = datePattern + " HH:mm:ss";

    /*
    日期转为字符串
     */
    public static final String date2Str(Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";
        if (aDate != null) {
            df = new SimpleDateFormat(timePattern);
            returnValue = df.format(aDate);
        }
        return (returnValue);
    }

    /*
    日期转为字符串
     */
    public static final String date2Str(String pattern, Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";
        if (aDate != null) {
            df = new SimpleDateFormat(pattern);
            returnValue = df.format(aDate);
        }
        return (returnValue);
    }

    /*
    字符串转为日期，失败返回null
     */
    public static final Date str2Date(String pattern, String strDate) {
        SimpleDateFormat df;
        Date date = null;
        df = new SimpleDateFormat(pattern);

        try {
            if (StringUtils.isEmpty(strDate)) {
                return null;
            }
            date = df.parse(strDate);
        } catch (Exception pe) {

        }
        if (date == null) {
            date = DateHelper.str2Date02(timePattern, strDate);
        }
        if (date == null) {
            date = DateHelper.str2Date02(datePattern, strDate);
        }
        if (date == null) {
            date = DateHelper.str2Date02(datePattern2, strDate);
        }
        if (date == null) {
            date = new Date(TConverter.ObjectToLong(strDate));
        }
        return (date);
    }

    public static final Date str2Date02(String pattern, String strDate) {
        SimpleDateFormat df;
        Date date = null;
        df = new SimpleDateFormat(pattern);

        try {
            if (StringUtils.isEmpty(strDate)) {
                return null;
            }
            date = df.parse(strDate);
        } catch (Exception pe) {

        }
        return (date);
    }

    /*
    字符串转为日期，失败返回null
     */
    public static final Date str2Date(String strDate) {
        SimpleDateFormat df;
        Date date = null;
        df = new SimpleDateFormat(timePattern);

        try {
            if (StringUtils.isEmpty(strDate)) {
                return null;
            }
            date = df.parse(strDate);
        } catch (Exception pe) {

        }
        if (date == null) {
            date = DateHelper.str2Date02(datePattern, strDate);
        }
        if (date == null) {
            date = DateHelper.str2Date02(datePattern2, strDate);
        }
        if (date == null) {
            date = DateHelper.str2Date02(datePattern3, strDate);
        }
        if (date == null) {
            date = DateHelper.str2Date02(datePattern4, strDate);
        }
        if (date == null) {
            date = DateHelper.str2Date02(datePattern5, strDate);
        }
        if (date == null) {
            date = new Date(TConverter.ObjectToLong(strDate));
        }
        return (date);
    }

    //获取当前时间
    public static String getTimeNow() {
        Date nowTime = new Date();
        return date2Str(nowTime);
    }

    //获取当前时间
    public static String getTimeNow(String pattern) {
        Date nowTime = new Date();
        return date2Str(pattern, nowTime);
    }

    //增加日
    public static Date addDays(Date date, int days) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + days);
        return now.getTime();
    }


    public static Date addYear(Date date, int year) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.YEAR, year);
        return now.getTime();
    }

    //获取当天开始时间
    public static Date getDayStartTime(Date date) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(date);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    //获取当天结束时间
    public static Date getDayEndTime(Date date) {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTime(date);
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    /***
     * 获取当前时间的前部分
     * @return
     */
    public static String getStringDateShort(Date currentTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /***
     * 获取当前时间的后部分
     * @return
     */
    public static String getTimeShort(Date currentTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    //获取时间戳
    public static String getTimestamp(Date date, int expireTime) {
        SimpleDateFormat formatter = new SimpleDateFormat(timePattern);
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.SECOND, expireTime);
        long ts = now.getTime().getTime();
        return String.valueOf(ts);
    }

    public static Date addSecond(Date date, int expireTime) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.SECOND, expireTime);
        return now.getTime();
    }

    //增加分
    public static Date addMinute(Date date, int expireTime) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.MINUTE, expireTime);
        return now.getTime();
    }

    //增加月
    public static Date addMonth(Date date, int month) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.MONTH, month);
        return now.getTime();
    }

    //获取月初
    public static Date getMonthBeginTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    //获取月末
    public static Date getMonthEndTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return addSecond(calendar.getTime(), -1);
    }

    //获取日期的天
    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DATE);
    }

    //获取年
    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    //获取月
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    //{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}
    public static int getWeekDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    public static Date getNextDayOfWeek(Date date, int dayOfWeek) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        int todayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        int nextMondayOffset = todayOfWeek == dayOfWeek ? 7 : (todayOfWeek > dayOfWeek ? 7 - todayOfWeek + dayOfWeek : dayOfWeek - todayOfWeek);
        cd.add(Calendar.DAY_OF_MONTH, nextMondayOffset);
        return cd.getTime();
    }

    public static String getPreQuarterOrYear(String time) throws Exception {
        String[] timeParts = time.split("-");
        if (timeParts.length == 2){
            int year = TConverter.ObjectToInt(timeParts[0]);
            int quarter = TConverter.ObjectToInt(timeParts[1]);
            if (quarter < 1 || quarter > 4)
                throw new Exception("获取上季度时间格式出错,季度大于4");
            int quarterNum = year * 4 + quarter;
            int preQuarterNum = quarterNum - 1;
            year = preQuarterNum / 4;
            quarter = preQuarterNum % 4;
            if (quarter == 0) {
                year = year - 1;
                quarter = 4;
            }

            return year + "-" + quarter;
        }else
            return String.valueOf(Integer.parseInt(time)-1);
    }

    public static int getQuarterNum(String time) throws Exception {
        String[] timeParts = time.split("-");
        if (timeParts.length != 2)
            throw new Exception("获取上季度时间格式出错,季度大于4");
        int year = TConverter.ObjectToInt(timeParts[0]);
        int quarter = TConverter.ObjectToInt(timeParts[1]);
        if (quarter < 1 || quarter > 4)
            throw new Exception("获取上季度时间格式出错,季度大于4");
        return year * 4 + quarter;
    }


    public static void main(String[] args) throws Exception {
//        System.out.println(getPreQuarter("2022-0"));
    }



}
